//
//  CHSLayerContentDetailController.m
//  ZXAnimationUtil_Example
//
//  Created by 朱新明 on 2023/9/10.
//  Copyright © 2023 simon. All rights reserved.
//

#import "CHSLayerContentDetailController.h"
#import <ZXCategoriesUtil/CAICategories.h>
#import <Masonry/Masonry.h>
#import "CHSContentLayerBackedView.h"
#import "CHSContentLayer.h"


@interface CHSLayerContentDetailController ()<CALayerDelegate>

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) CHSContentLayerBackedView *layerBackedView;



@property (nonatomic, strong) CALayer *layer;

@property (nonatomic, strong) CHSContentLayer *customLayer;


@end

@implementation CHSLayerContentDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设置图层内容";
    self.view.backgroundColor = [UIColor zx_colorWithHexString:@"#FDFEFF"];
    
    // Do any additional setup after loading the view.
    if (self.type == 0) {
        [self contentsLoad];
    }
    else if (self.type == 1){
        [self delegateLoad];
    }
    else if (self.type == 2){
        [self addLayerBackedViewDelegateLoad];
    }
    else if (self.type == 3){
        [self addCustomLayer];
    }
}

#pragma mark - contents

- (void)contentsLoad
{
    [self.view addSubview:self.contentView];
    self.contentView.layer.contents = (__bridge id _Nullable)(([UIImage imageNamed:@"keyframing_2"]).CGImage);
}

- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [UIView new];
        _contentView.layer.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5].CGColor;
        _contentView.layer.frame = CGRectMake(0,100, UIScreen.mainScreen.bounds.size.width, 100);
    }
    return _contentView;
}


#pragma mark - delegate

- (void)delegateLoad{
    [self.view.layer addSublayer:self.layer];
    [self.layer setNeedsDisplay];
}

- (CALayer *)layer{
    if (!_layer) {
        _layer = [[CALayer alloc]init];
        _layer.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5].CGColor;
        _layer.frame = CGRectMake(0,100, UIScreen.mainScreen.bounds.size.width, 100);
        _layer.delegate = self;
    }
    return _layer;
}


///设置contents方式提供静态图像位图：系统没有创建图形上下文。该实现负责创建位图并将其赋值给图层的contents属性。
- (void)displayLayer:(CALayer *)layer{
    layer.contents = (__bridge id _Nullable)(([UIImage imageNamed:@"keyframing_2"]).CGImage);
}

///图形上下文方式提供动态绘制位图：系统创建了一个图形上下文。在方法中绘制位图到图形上下文中。<CGContext 0x600003cc1200> (kCGContextTypeCoreAnimationAutomatic)
//如果你没有预渲染的image（图像）或辅助对象来为你创建位图，你的委托可以使用drawLayer:inContext:方法动态绘制内容。
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    CGMutablePathRef thePath = CGPathCreateMutable();
    CGPathMoveToPoint(thePath,NULL,15.0f,15.f);
    CGPathAddCurveToPoint(thePath,
                          NULL,
                          15.f,250.0f,
                          295.0f,250.0f,
                          295.0f,15.0f);
 
    CGContextBeginPath(ctx);
    CGContextAddPath(ctx, thePath);
    // Release the path
    CFRelease(thePath);
    
    CGContextSetLineWidth(ctx, 5);
    CGContextStrokePath(ctx);
}




#pragma mark - delegate2

- (void)addLayerBackedViewDelegateLoad{
    [self.view addSubview:self.layerBackedView];
}

- (CHSContentLayerBackedView *)layerBackedView{
    if (!_layerBackedView) {
        CHSContentLayerBackedView *view = [[CHSContentLayerBackedView alloc] init];
        view.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
        view.frame = CGRectMake(0,100, UIScreen.mainScreen.bounds.size.width, 100);
        _layerBackedView = view;
    }
    return _layerBackedView;
}


#pragma mark - 子类化图层

- (void)addCustomLayer{
    [self.view.layer addSublayer:self.customLayer];
    [self.customLayer setNeedsDisplay];
}

- (CHSContentLayer *)customLayer{
    if (!_customLayer) {
        CHSContentLayer *layer = [CHSContentLayer layer];
        layer.backgroundColor = [UIColor orangeColor].CGColor;
        layer.frame = CGRectMake(0,100, UIScreen.mainScreen.bounds.size.width, 100);
        _customLayer = layer;
    }
    return _customLayer;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
